package projbd;

import java.io.IOException;
import java.sql.Statement;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleEntry;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

public class XMLStateReconstructor {
    
    private Writer xmlWriter;
    private String orderStatement;
    private Statement statement;
    private Connection connection;
    
    private Stack<SimpleEntry<Integer,String>> tagStack = new Stack<SimpleEntry<Integer,String>>();
    
    private int current_parent_id = 0;
    
    
    public XMLStateReconstructor(String file) throws UnsupportedEncodingException, FileNotFoundException, SQLException, IOException {
                
        connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/projbd", "postgres", "srx3325");
        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        xmlWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        orderStatement = "SELECT * FROM order_size_edges_2 ORDER BY id";
    }
    
    public void reconstruct() throws SQLException, IOException {
        ResultSet result = statement.executeQuery(orderStatement);
        
        result.first();
        while (!result.isAfterLast()) {
            
            int id = result.getInt("id");
            int parent_id = result.getInt("parent_id");
            String value = result.getString("value");
            String datatype = result.getString("datatype");
            
            // close tags
            if(parent_id < current_parent_id) {
                
                SimpleEntry<Integer,String> tag;
                tag = tagStack.pop();
                
                while(tag.getKey().intValue() > parent_id) {
                    xmlWriter.write("<\\" + tag.getValue() + ">\n");
                    tag = tagStack.pop();
                }
                tagStack.push(tag);
               
            } 
            current_parent_id = parent_id;           
  
            if (datatype.equals("tag")) {              
               tagStack.push(new SimpleEntry(new Integer(id), value));
               xmlWriter.write("<" + value + ">\n");               
            } else {              
                xmlWriter.write(value + "\n");
            }
   
            result.next();
            xmlWriter.flush();
        }
        
        while(! tagStack.empty())
            xmlWriter.write("<\\" + tagStack.pop().getValue() + ">\n");
              
        xmlWriter.flush();
        xmlWriter.close();
    }
    
    public static void main(String[] args) {
        
        try {
            XMLStateReconstructor xmlReconstructor = new XMLStateReconstructor("C:\\bd\\hamlet-reconstructed.xml");
            xmlReconstructor.reconstruct();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(XMLStateReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLStateReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(XMLStateReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLStateReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
